package com.Small;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.Adapter.SmallAppGalleryAdapter;
import com.StaticValue.StaticValue;
import com.UIManager.UIManager;
import com.Welcome.R;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RadioButton;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.SlidingDrawer.OnDrawerScrollListener;
import android.widget.TextView;
import android.widget.Toast;

public class AppTest extends Activity {

	private TextView textView = null;
	private Button button = null;

	private ImageView scr1;

	private ImageView all;

	private LinearLayout l1;

	private LayoutInflater mInflater;

	private TextView temp;

	private float width;

	private int currIndex = 0;

	View Test2;
	View Test1;
	View Test3;
	View Test4;

	LinearLayout linearLayoutAll;

	final static String FATHERACCOUNT = "fatherAccount";

	final static String CHILDACCOUNT = "childAccount";

	final static String BALANCEACCOUNT = "accountBalance";

	private GridView gvTopBar;

	private SlidingDrawer slidingDrawer;

	private ScrollLayout mRoot;

	public String scrollFlg;
	List<String> accountFather = new ArrayList<String>();

	List<List<Map<String, String>>> accountChild = new ArrayList<List<Map<String, String>>>();

	// AccountExpandablebaseadapter adapter;

	// ExpandableListView listAccount;

	// ExpandableListView listAccount1;

	static ImageAdapter topImgAdapter;

	private UIManager manager = null;
	private SmallAppGalleryAdapter adapter = null;
	private RadioButton radioButton = null;
	private int index = 0;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			super.handleMessage(msg);
			if (msg.what == StaticValue.UPDATERADIOGROUP) {
				radioButton = (RadioButton) manager.getSmallAppRadioGroup()
						.getChildAt(index);
				radioButton.setChecked(true);
			}
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smallapp);
		adapter = new SmallAppGalleryAdapter(AppTest.this);
		manager = new UIManager(this);

		manager.getSmallAppGalery().setAdapter(adapter);
		manager.getSmallAppGalery().setOnItemClickListener(
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int position, long arg3) {

						Toast.makeText(AppTest.this, "Position" + position,
								Toast.LENGTH_SHORT).show();
					}
				});
		manager.getSmallAppGalery().setOnItemSelectedListener(
				new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int position, long arg3) {
						index = position;
						handler.sendEmptyMessage(StaticValue.UPDATERADIOGROUP);
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});
		mRoot = (ScrollLayout) findViewById(R.id.mRoot);

		DisplayMetrics dm = new DisplayMetrics();

		this.getWindowManager().getDefaultDisplay().getMetrics(dm);
		width = dm.widthPixels;

		fillViews();

		scr1 = new ImageView(this);
		scr1.setBackgroundColor(Color.TRANSPARENT);
		scr1.setBackgroundResource(R.drawable.scr);
		all = new ImageView(this);

		all.setBackgroundResource(R.drawable.slidbackgroup);

		l1 = (LinearLayout) findViewById(R.id.l1);

		l1.setBackgroundResource(R.drawable.all);
		l1.addView(scr1, new LayoutParams((int) width / 4, 6));

		mInflater = LayoutInflater.from(this);
		Test1 = mInflater.inflate(R.layout.test1, null);
		Test2 = mInflater.inflate(R.layout.test2, null);
		Test3 = mInflater.inflate(R.layout.test3, null);
		Test4 = mInflater.inflate(R.layout.test4, null);
		// ScrollView s1 = (ScrollView)Test1.findViewById(R.id.s1);

		View convertView = mInflater.inflate(R.layout.slidingdrawer, null);

		LinearLayout ll = new LinearLayout(this);
		ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));

		ll.setVerticalGravity(Gravity.BOTTOM);
		ll.setGravity(Gravity.BOTTOM);

		ll.addView(convertView);

		linearLayoutAll = (LinearLayout) findViewById(R.id.linearLayoutAll);

		slidingDrawer = (SlidingDrawer) convertView
				.findViewById(R.id.slidingdrawer);

		slidingDrawer.setOnDrawerScrollListener(new OnDrawerScrollListener() {

			@Override
			public void onScrollStarted() {
				// temp.setVisibility(View.GONE);
				linearLayoutAll.setVisibility(View.VISIBLE);
			}

			@Override
			public void onScrollEnded() {

			}
		});

		slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

			@Override
			public void onDrawerClosed() {
				// temp.setVisibility(View.VISIBLE);

			}
		});

		slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {

			@Override
			public void onDrawerOpened() {
				// linearLayoutAll.getBackground().setAlpha(50);
				// linearLayoutAll.invalidate();
				// linearLayoutAll.setVisibility(View.GONE);
			}
		});
		this.addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));

		mRoot.addView(Test2);
		mRoot.addView(Test1);
		mRoot.addView(Test3);
		mRoot.addView(Test4);

		textView = (TextView) mRoot.findViewById(R.id.sildTextOne);

		button = (Button) mRoot.findViewById(R.id.sildButton);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				textView.setText("Come from Button");

			}
		});

	}

	public void translateAnimation(int curr, int start, int end) {
		TranslateAnimation animation;
		if (currIndex != curr) {
			scr1.setBackgroundColor(Color.TRANSPARENT);
			scr1.setBackgroundResource(R.drawable.scr);
			if (curr == 3) {
				animation = new TranslateAnimation((int) (width / 4)
						* (Math.abs(currIndex)), (int) (width / 4 * end) - 7,
						0, 0);
			} else if (curr == 2) {
				animation = new TranslateAnimation((int) (width / 4)
						* (Math.abs(currIndex)), (int) (width / 4 * end) - 5,
						0, 0);
			} else {
				animation = new TranslateAnimation((int) (width / 4)
						* (Math.abs(currIndex)), (int) (width / 4 * end) - 3,
						0, 0);
			}
			animation.setInterpolator(new LinearInterpolator());
			animation.setDuration(20);
			animation.setFillAfter(true);
			scr1.startAnimation(animation);
			currIndex = curr;
		}
	}

	private void fillViews() {
		// ***
		int[] topbar_image_array = { R.drawable.a, R.drawable.b, R.drawable.c,
				R.drawable.d };

		gvTopBar = (GridView) this.findViewById(R.tab.gridview);
		gvTopBar.setNumColumns(4);
		gvTopBar.setSelector(new ColorDrawable(Color.TRANSPARENT));
		// gvTopBar.setGravity(Gravity.CENTER);
		gvTopBar.setVerticalSpacing(0);
		int width = this.getWindowManager().getDefaultDisplay().getWidth() / 4;
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		topImgAdapter = new ImageAdapter(this, topbar_image_array, width,
				Common.dip2px(35, dm), R.drawable.ccc);
		gvTopBar.setAdapter(topImgAdapter);
		gvTopBar.setOnItemClickListener(new ItemClickEvent());
		topImgAdapter.SetFocus(0);

	}

	class ItemClickEvent implements OnItemClickListener {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			translateAnimation(arg2, arg2, arg2);
			topImgAdapter.SetFocus(arg2);
			mRoot.skip(arg2);
		}
	}

	void SwitchActivity(int id) {
	}

}